package com.gqz.patrol.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gqz.common.core.domain.R;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.patrol.api.EnPatrolTaskApi;
import com.gqz.patrol.app.mapstruct.EnPatrolTaskMapstruct;
import com.gqz.patrol.app.service.IEnPatrolTaskOperateService;
import com.gqz.patrol.app.service.MqEnPatrolTaskService;
import com.gqz.patrol.app.utils.GenTaskNoTool;
import com.gqz.patrol.service.IEnPatrolTaskService;
import com.gqz.patrol.vo.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * 能源-巡视工单表 Controller
 */
@RestController
@RequestMapping("/en-patrol-task")
public class EnPatrolTaskController implements EnPatrolTaskApi {

    @Autowired
    private IEnPatrolTaskService service;
    @Autowired
    private ObjectMapper objectMapper;
    @Resource
    private MqEnPatrolTaskService mqService;
//    @Resource
//    private GisFegin gisFegin;

    @Autowired
    private IEnPatrolTaskOperateService operateService;

    @Resource
    private EnPatrolTaskMapstruct ennPatrolTaskMapstruct;

    @Override
    public R<List<EnPatrolTaskVo>> getById(String id) {
        return R.ok(service.getById(id));
    }

    @Override
    public R<List<EnPatrolTaskVo>> getByTaskNo(String taskNo) {
        return R.ok(service.getByTaskNo(taskNo));
    }

    @Override
    public R<Page<EnPatrolTaskVo>> page(@Valid RequestForm<EnPatrolTaskVo> requestForm) {
        return R.ok(service.page(requestForm));
    }

    @Override
    public R<Long> count(@Valid RequestForm<EnPatrolTaskVo> requestForm) {
        return R.ok(service.count(requestForm));
    }

    @Override
    public R<List<String>> insertV2(@Valid List<EnPatrolTaskVo> vos) {
        List<EnPatrolTaskVo> temp = new ArrayList<>();
        vos.forEach(vo -> {
            EnPatrolTaskVo taskForm = ObjectUtil.clone(vo);
            temp.add(taskForm);
        });
        List<String> taskNos = mqService.insertV2(temp);

        R<List<String>> r = new R<>();
        r.setMsg("成功");
        r.setCode(200);
        r.setData(taskNos);
        return r;
    }

    @Override
    public R<Integer> update(@Valid List<EnPatrolTaskVo> vos) {
        return R.ok(this.mqService.update(vos));
    }

    @Override
    public R<Boolean> delete(@Valid List<String> ids) {
        return R.ok(service.removeBatchByIds(ids));
    }

    @Override
    public R<Integer> sendOrder(EnPatrolTaskVo vo) {
        this.service.saveTaskOperate(vo, "10", "已派单");
        return R.ok(this.mqService.sendOrder(vo));
    }

    @Override
    public R<List<EnPatrolTaskEquipVo>> getTaskEquipList(String taskNo) {
        return R.ok(service.getTaskEquipList(taskNo));
    }

    @Override
    public R<List<EnStockEquipDefectVo>> getTaskDefectList(String taskNo) {
        return R.ok(service.getTaskDefectList(taskNo));
    }

    @Override
    public R<EnPatrolTaskProcessVo> getTaskProcess(String taskNo) {
        return R.ok(service.getTaskProcess(taskNo));
    }

    @Override
    public R<List<EnStockEquipDefectVo>> getTaskHandleDefectList(String taskNo) {
        return R.ok(service.getTaskHandleDefectList(taskNo));
    }

    @Override
    public R<String> genTaskNo() {
        R<String> r = new R<>();
        r.setCode(200);
        r.setData(GenTaskNoTool.createTaskNo());
        return r;
    }

    @Override
    public void exportExcel(HttpServletResponse resp, RequestForm<EnPatrolTaskExcelVo> requestForm) {
        service.exportExcel(resp, requestForm);
    }

    @Override
    public R<List<EnGisVo>> energyPtgis(String startTime, String endTime, String yhid) {
//        return R.ok(gisFegin.list(startTime,endTime,yhid));
        return null;
    }

    @Override
    public List<EnTjVo> getGdStatusXx(String sdate, String edate) {
        return service.getGdStatusXx(sdate, edate);
    }
}
